
package me.ak.sf;

import java.util.concurrent.CopyOnWriteArrayList;
import me.ak.sf.level.Level;

import org.lwjgl.input.Keyboard;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import resources.Resources;


public class SlothfallGameState extends BasicGameState{
    
    public Level currentLevel;
    
     public  Image fallingSloth;
    public  Image turtle;

    public static CopyOnWriteArrayList<Sloth> sloths = new CopyOnWriteArrayList<Sloth>();

    
    public StateBasedGame parent;
    public Player thePlayer;

  
    

    public boolean playerFlipped = false;
    public float playerMovement = 0;

    
    @Override
    public void keyPressed(int key, char keyc) {
        if (key==Keyboard.KEY_LEFT) { 
            playerMovement = -0.5f;
            playerFlipped = true;
        }
         if (key==Keyboard.KEY_RIGHT) {
            playerMovement = 0.5f;
            playerFlipped = false;
        }
    }
    
    @Override
    public void keyReleased(int key, char keyc) {
        if ((key==Keyboard.KEY_LEFT || key==Keyboard.KEY_RIGHT) &&
                !(Keyboard.isKeyDown(Keyboard.KEY_LEFT)|| Keyboard.isKeyDown(Keyboard.KEY_RIGHT))) {
            playerMovement = 0f;
        }
    }

    @Override
    public int getID() {
        return 1;
    }

    @Override
    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
        parent = sbg;
       
        
        fallingSloth = Resources.FALLINGSLOTH;
        turtle = Resources.TURTLE;
        thePlayer = new Player(375, 400);
        
        currentLevel = Level.getLevel(0);
        currentLevel.init(this);
    }

    @Override
    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
        currentLevel.renderBackground(g);
        for (Sloth s : sloths) {
            g.drawImage(fallingSloth.getScaledCopy((int)s.getBounds().getWidth(), (int)s.getBounds().getHeight()), s.x, s.y);
        }
        g.drawImage(turtle.getScaledCopy(125, 125).getFlippedCopy(playerFlipped, false), thePlayer.x, thePlayer.y);
        currentLevel.renderForeground(g);
    }

    @Override
    public void update(GameContainer gc, StateBasedGame sbg, int i) throws SlickException {
        currentLevel.update();
        
        
    }

}
